package com.sywl.glzh.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sywl.glzh.domain.News;
import com.sywl.web.util.Page;
import com.sywl.web.util.SinglePage;

@Controller
public class NewController {
	
	/**
	 * 新闻动态
	 * @param 页码
	 * @param model
	 * @return
	 */
	@RequestMapping("new/{page}")
	public String news(@PathVariable int page, Model model) {
		Page p = new SinglePage((int)News.countNews(), page, 5, true);
		if(!p.isValid()) {
			return "redirect:new/1";
		}
		model.addAttribute("newses", News.fineSortEntities(p.getFirst(), p.getMaxResult()));
		return "new";
	}
	
	/**
	 * 新闻动态 英文
	 * @param 页码
	 * @param model
	 * @return
	 */
	@RequestMapping("en/new/{page}")
	public String enNews(@PathVariable int page, Model model) {
		String view = news(page, model);
		if (view.contains("redirect")) {
			return view;
		}
		return "en/" + view;
	}
	
	/**
	 * 产品详细
	 * @param 产品编号
	 * @param model
	 * @return
	 */
	@RequestMapping("new/view/{id}")
	public String newsView(@PathVariable long id, Model model) {
		News news = News.findNews(id);
		if(news == null) {
			return "redirect:/404";
		}
		model.addAttribute("news", news);
		model.addAttribute("prev", News.findNews(news.getId() - 1));
		model.addAttribute("next", News.findNews(news.getId() + 1));
		return "view";
	}
	
	/**
	 * 产品详细 英文
	 * @param 产品编号
	 * @param model
	 * @return
	 */
	@RequestMapping("en/new/view/{id}")
	public String enNewsView(@PathVariable long id, Model model) {
		String view = newsView(id, model);
		if (view.contains("redirect")) {
			return view;
		}
		return "en/" + view;
	}
}
